package com.sele.utils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 参考网址:http://blog.csdn.net/aqsunkai/article/details/52270198
 * @author Admin
 *
 */



public class CustomUtils {
	private final static String xls = "xls";  
    private final static String xlsx = "xlsx"; 
    private static final Logger logger = LoggerFactory.getLogger(CustomUtils.class);
    
    public static String concatPath(String fileName){
    	try {
			checkFile(fileName);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			logger.error("checkFile error");
		}
    	fileName = addSlash(fileName);
    	String filePath = "/data"+fileName;
		return filePath;
    	
    }
	/**
	 * path不以’/'开头时，默认是从此类所在的包下取资源；
		path  以’/'开头时，则是从ClassPath根下获取；
	 * @param filename
	 * @return
	 */
	public static String getPath(String fileName){
		String filePath =concatPath(fileName);
		URL url = CustomUtils.class.getResource(filePath);
		return url.toString();
		
	}
	
	public static String addSlash(String filePath){
		if(!filePath.startsWith("/")){
			filePath = "/"+filePath;
		}
		return filePath;
		
	}
	
	public static InputStream getInputStreamFromFile(String fileName){
		String filePath =concatPath(fileName);
		 // 将属性文件转换为属性对象
        InputStream is = CustomUtils.class.getResourceAsStream(filePath);

        if (is == null) {
            throw new RuntimeException("初始化失败:" +
                "请检查属性文件"+filePath+"是否在输出类路径下");
        }
        return is;

       
	}
  
	/**
	 * 兼容xls格式和xlsx格式
	 * @param fileName
	 * @return
	 */
	public static Workbook getWorkBook(String  fileName) {  
        
        //创建Workbook工作薄对象，表示整个excel  
        Workbook workbook = null;  
        try {  
            //获取excel文件的io流  
            InputStream is = getInputStreamFromFile(fileName);  
            //根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象  
            if(fileName.endsWith(xls)){  
                //2003  
                workbook = new HSSFWorkbook(is);  
            }else if(fileName.endsWith(xlsx)){  
                //2007  
                workbook = new XSSFWorkbook(is);  
            }  
        } catch (IOException e) {  
            logger.info(e.getMessage());  
        }  
        return workbook;  
    } 
   
	
	
	public static String getCellValue(Cell cell){  
        String cellValue = "";  
        if(cell == null){  
            return cellValue;  
        }  
        //把数字当成String来读，避免出现1读成1.0的情况  
        if(cell.getCellTypeEnum() == CellType.NUMERIC){  
            cell.setCellType(CellType.STRING);  
        }  
        //判断数据的类型  
        switch (cell.getCellTypeEnum()){  
            case NUMERIC: //数字  
                cellValue = String.valueOf(cell.getNumericCellValue());  
                break;  
            case STRING: //字符串  
                cellValue = String.valueOf(cell.getStringCellValue());  
                break;  
            case BOOLEAN: //Boolean  
                cellValue = String.valueOf(cell.getBooleanCellValue());  
                break;  
            case FORMULA: //公式  
                cellValue = String.valueOf(cell.getCellFormula());  
                break;  
            case BLANK: //空值   
                cellValue = "";  
                break;  
            case ERROR: //故障  
                cellValue = "非法字符";  
                break;  
            default:  
                cellValue = "未知类型";  
                break;  
        }  
        return cellValue;  
    }  
   public static void checkFile(String fileName) throws IOException{  
       //判断文件是否存在  
       if(!StringUtils.isNotBlank(fileName)){  
           logger.error("文件不存在！");  
           throw new FileNotFoundException("文件不存在！");  
       }  
       //获得文件名  
       
       //判断文件是否是excel文件  
       if(!fileName.endsWith(xls) && !fileName.endsWith(xlsx)){  
           logger.error(fileName + "不是excel文件");  
           throw new IOException(fileName + "不是excel文件");  
       }  
   }  
  
   
   public static void sleep(int time){
	   try {
           Thread.sleep(time);
       } catch (InterruptedException e) {
           // TODO Auto-generated catch block
    	   logger.error(e.getMessage());
       }
   }
   
   
   
   /**
	 * 关闭浏览器的方法
	 * @param driver
	 */
	public static void close(WebDriver driver){
		//close只关闭当前页面
		
		
		//退出driver中的所有窗口
		try{
			//firefox插件默认退出最后一个窗口后，会自动quit
			driver.close();
			driver.quit();
		}catch (Exception e) {
			// 说明已经关闭
		}
	}
   
   public static void main(String[] args) {
	   String filePath = "data/login.xlsx";
	   String getPath = getPath("data/login.xlsx");
	   System.out.println("getPath:"+getPath);
	   InputStream is = getInputStreamFromFile(filePath);
	   if(is!=null){
		   try {
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   }
   }
   
   
   
}
